﻿using System.Collections.Generic;
using ApplicationLibrary;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Fighter.Masters.ModuleData;
using Fighter.Fights.ModuleData;

namespace Fighter.Fights.Forms
{
    public partial class Fight : ApplicationLibrary.DBManager.DocumentForm
    {
        private BindableOject<int> ID = null;
        private DUFight duFight = null;

        public Fight()
            : base("Fight")
        {
            InitializeComponent();
        }

        #region Virtual Method

        protected override IRadarParameters GetKeyFromDocument()
        {
            return new RadarMatchParam(ID.Value);
        }

        protected override void OnAttachData()
        {
            dbManager = new dbmanagerMatch("matchForm", new RadarMatch(), this);
            dbManager.AddMaster(this, FG_Fight.Name, true);

            ID = new BindableOject<int>();
            ID.AttachCounterType(Counters.Match.Int(), GlobalInfo.CurrentDate, null);

            duFight = new DUFight();
        }

        protected override void OnBindData()
        {
            BindObject(ID, FG_Fight.ID);
            BindControl(ckbActive, FG_Fight.Active, "DBChecked");
            BindControl(txtDescription, FG_Fight.Description);

            BindCounter(ID);
        }

        protected override void FocusOnNew()
        {
            txtDescription.Focus();
        }

        protected override bool OnAfterSave()
        {
            QueryBuilder qb = new QueryBuilder();
            SqlABParameter sqlP1 = new SqlABParameter("@p1", FG_Fight.ID);
            sqlP1.Value = ID.Value;

            qb.Update(FG_Fight.Name);
            qb.AddSet<bool>(FG_Fight.Active, false);
            qb.AddCompare(FG_Fight.ID, sqlP1, "!=");

            SqlABCommand sqlCM = new SqlABCommand(qb.Query, GlobalInfo.DBaseInfo.dbManager.DB_Connection);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.ExecuteNonQuery();

            return base.OnAfterSave();
        }

        #endregion
    }

    #region DbManager dbmanagerDescriptions

    /// <summary>
    /// Classe derivata per gestioni particolari
    /// </summary>
    internal class dbmanagerMatch : DBManager
    {
        public dbmanagerMatch(string name, RadarForm radar, DocumentForm document)
            : base(name, radar, document)
        { }

        protected override void dAdapter_MasterRowUpdating(object sender, SqlABRowUpdatingEventArgs e)
        {
            base.dAdapter_MasterRowUpdating(sender, e);
        }

        protected override string CreateMasterQuery()
        {
            this.Table = new FG_Fight();
            QueryBuilder qB = new QueryBuilder();
            qB.AddSelect();
            qB.AddFrom(this.Table);
            qB.AddCompare(FG_Fight.ID, "@p1");

            return qB.Query;
        }

        protected override List<SqlABParameter> CreateMasterParam()
        {
            List<SqlABParameter> PList = new List<SqlABParameter>();

            SqlABParameter sParam1 = new SqlABParameter("@p1", FG_Fight.ID);
            sParam1.Value = FG_Fight.ID.DefaultValue;
            PList.Add(sParam1);
            return PList;
        }

        protected override void SetParameters(ApplicationLibrary.ExtendedControls.IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }
    }

    #endregion
}
